<template>
  <div class="box">
    <div class="searchheader van-hairline--bottom">
      <van-search
        v-model="value"
        show-action
        placeholder="请输入搜索关键词"
        shape="round"
        @focus="$router.back()"
      >
        <template #action>
          <van-button
            class="searchbtn"
            type="primary"
            round
            size="mini"
            color="linear-gradient(to right, #ff6034, #ee0a24)"
            >搜索</van-button
          >
        </template>
        <template #left>
          <van-icon name="arrow-left" size="26" @click="$router.back()" />
        </template>
      </van-search>
    </div>
    <div class="main"  @scroll="scrollMe" ref="main">
      <div class="hasthing" v-if="flag">
        <div class="top10">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <ul :class="isShow ? 'proList2' : 'proList1'">
              <li
                class="van-hairline--bottom van-hairline--right"
                v-for="item in searchList"
                :key="item.proid"
                @click="jumpTo(item.proid)"
              >
                <img width="100%" :src="item.img1" alt="" />
                <div>
                  <h4>{{ item.proname }}</h4>
                  <p>￥{{ item.originprice.toFixed(2) }}</p>
                  <span>销量{{ item.sales }}</span>
                </div>
                <b>看相似</b>
              </li>
              <div class="toggle" @click="isShow = !isShow">
                <van-icon name="bars" v-if="isShow" />
                <van-icon name="apps-o" v-else />
              </div>
            </ul>
          </van-list>
        </div>

        <div class="top" v-if="scrollTop > 700" @click="backTop">
          <van-icon name="back-top" />
        </div>
      </div>

      <div class="nothing" v-else>
        <h3>抱歉，暂未找到相关信息</h3>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategoryBrandProList } from "../api/kind";
export default {
  data() {
    return {
      value: "",
      searchList: [],
      flag: true,
      isShow: true,
      scrollTop: 0,
      loading: false,
      finished: false,
      count:2,
      timer: null,
    };
  },
  methods: {
    backTop() {
      this.timer = setInterval(() => {
        if (this.$refs.main.scrollTop <= 0) {
          clearInterval(this.timer);
        }
        this.$refs.main.scrollTop -= 100;
      }, 15);
    },
    onLoad() {
      this.loading = true;
      setTimeout(() => {
        getCategoryBrandProList({ count: this.count, limitNum: 12 ,category:this.$route.params.category,brand:this.$route.params.brand}).then((res) => {
          console.log(res);
          this.loading = false;
          if (res.data.code == "10003") {
            this.finished = true;
          } else {
            this.searchList = [...this.searchList, ...res.data.data]; //this.proList=原有的数据加新加载的数据
            this.count++;
          }
        });
      }, 1000);
    },
    scrollMe() {
      this.scrollTop = this.$refs.main.scrollTop;
    },
    jumpTo(proid) {
      this.$router.push("/detail/" + proid);
      // this.$router.push({path:"/detail/",params:{proid}})
    },
  },
  created() {
    getCategoryBrandProList({
      count: 1,
      limitNum: 12,
      category:this.$route.params.category,
      brand:this.$route.params.brand,
    }).then((res) => {
      console.log(res);
      this.value = this.$route.params.brand;
      switch (res.data.code) {
        case "10003":
          this.flag = false;
          break;
        case "200":
          this.searchList = res.data.data;
          break;
        default:
          break;
      }
    });
  },
};
</script>

<style>
.proList1 {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.proList1 li {
  width: 44%;
  height: 2.4rem;
  margin: 0.1rem 0.1rem;
  position: relative;
}
.proList1 li img {
  width: 80%;
  height: 50%;
  margin: 0.2rem;
  border-radius: 0.06rem;
}
.proList1 li h4 {
  width: 100%;
  font-size: 0.14rem;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.proList1 li p {
  color: rgb(201, 22, 35);
  bottom: 0.1rem;
}
.proList1 li span {
  color: #999;
  font-size: 0.14rem;
}
.proList1 li b {
  font-size: 0.1rem;
  position: absolute;
  right: 0.1rem;
  bottom: 0.01rem;
  background: rgb(225, 225, 225);
  width: 0.5rem;
  height: 0.2rem;
  border-radius: 0.1rem;
  text-align: center;
  line-height: 0.2rem;
}
.proList2 li {
  display: flex;
  height: 1rem;
  padding: 0.1rem 0.1rem;
}
.proList2 li img {
  width: 30%;
  height: 90%;
  margin: 0.1rem;
  border-radius: 0.06rem;
}
.proList2 li div {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
.proList2 li h4 {
  width: 100%;
  font-size: 0.14rem;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.proList2 li p {
  color: rgb(201, 22, 35);
}
.proList2 li div span {
  color: #999;
  font-size: 0.14rem;
}
.proList2 li b {
  font-size: 0.1rem;
  position: absolute;
  right: 0.1rem;
  bottom: 0.02rem;
  background: rgb(225, 225, 225);
  width: 0.5rem;
  height: 0.2rem;
  border-radius: 0.1rem;
  text-align: center;
  line-height: 0.2rem;
}
.toggle {
  width: 0.3rem;
  height: 0.3rem;
  border-radius: 50%;
  position: fixed;
  bottom: 0.5rem;
  right: 0.2rem;
  text-align: center;
  line-height: 0.3rem;
  background: rgb(201, 22, 35);
  font-size: 0.18rem;
  color: #fff;
}
.top {
  width: 0.3rem;
  height: 0.3rem;
  border-radius: 50%;
  position: fixed;
  bottom: 1rem;
  right: 0.2rem;
  text-align: center;
  line-height: 0.3rem;
  background: rgb(201, 22, 35);
  font-size: 0.18rem;
  color: #fff;
}
</style>